package com.sojson.project.pay.controller.wx;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.project.dao.HotelOrderDao;
import com.sojson.project.dao.OrderChildDao;
import com.sojson.project.dao.OrderDao;
import com.sojson.project.dao.OrderDepositDao;
import com.sojson.project.entity.po.HotelOrder;
import com.sojson.project.entity.po.Order;
import com.sojson.project.entity.po.OrderChild;
import com.sojson.project.entity.po.OrderDeposit;
import com.sojson.project.pay.service.wx.NotifyPayWxService;
import com.sojson.util.StringUtil;
import com.sojson.util.pay.wx.PayWxParamNofityVerifyInterface;
import com.sojson.util.pay.wx.bean.WxCallBackBean;
import com.sojson.util.pay.wx.bean.WxResponseV3;
import com.sojson.util.pay.wx.constant.WxConstant;

import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 微信支付回调控制器
 * 
 * @author liu
 * @date 2021-03-24
 */
@Api(tags = "微信支付回调管理")
@ApiIgnore
@RestController
@RequestMapping(value = "/notify/pay/wx")
public class NotifyPayWxController {

    @Autowired
    private NotifyPayWxService notifyPayWxService;
    @Autowired
    private PayWxParamNofityVerifyInterface paramNofityVerifyInterface;
    @Resource
    private OrderDao orderDao;
    @Resource
    private OrderChildDao orderChildDao;
    @Resource
    private OrderDepositDao orderDepositDao;
    @Resource
    private HotelOrderDao hotelOrderDao;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;

    /**
     * 支付回调(商城)
     * 
     * @return
     */
    @RequestMapping("/goods")
    public WxCallBackBean goods() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            if (!WxConstant.SUCCESS.equals(paramResult.getTradeState())) {
                Order order = new Order();
                order.updateInit();
                order.setNo(paramResult.getOutTradeNo());
                order.setPayMsg(paramResult.getTradeStateDesc());
                orderDao.updatePartialByOrderNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPay = notifyPayWxService.goods(paramResult);
            if (!notifyPay) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 退款回调(商城)
     * 
     * @return
     */
    @RequestMapping("/goodsCancel")
    public WxCallBackBean goodsCancel() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            String refundStatus = paramResult.getRefundStatus();
            if (!WxConstant.SUCCESS.equals(refundStatus)) {
                Order order = new Order();
                order.updateInit();
                order.setNo(paramResult.getOutTradeNo());
                order.setRefundsMsg(refundStatus);
                orderDao.updatePartialByOrderNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayWxService.goodsCancel(paramResult);
            if (!notifyPayCancel) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 支付回调(诚意金)
     * 
     * @return
     */
    @RequestMapping("/deposit")
    public WxCallBackBean deposit() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            if (!WxConstant.SUCCESS.equals(paramResult.getTradeState())) {
                OrderDeposit order = new OrderDeposit();
                order.updateInit();
                order.setOrderSn(paramResult.getOutTradeNo());
                order.setPayMsg(paramResult.getTradeStateDesc());
                orderDepositDao.updatePartialByOrderNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPay = notifyPayWxService.deposit(paramResult);
            if (!notifyPay) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 退款回调(诚意金)
     * 
     * @return
     */
    @RequestMapping("/depositCancel")
    public WxCallBackBean depositCancel() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            String refundStatus = paramResult.getRefundStatus();
            if (!WxConstant.SUCCESS.equals(refundStatus)) {
                OrderDeposit order = new OrderDeposit();
                order.updateInit();
                order.setOrderSn(paramResult.getOutTradeNo());
                order.setRefundsMsg(refundStatus);
                orderDepositDao.updatePartialByOrderNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayWxService.depositCancel(paramResult);
            if (!notifyPayCancel) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 支付回调(酒店)
     * 
     * @return
     */
    @RequestMapping("/hotel")
    public WxCallBackBean hotel() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            if (!WxConstant.SUCCESS.equals(paramResult.getTradeState())) {
                HotelOrder order = new HotelOrder();
                order.updateInit();
                order.setNo(paramResult.getOutTradeNo());
                order.setMsgPay(paramResult.getTradeStateDesc());
                hotelOrderDao.updateByNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayWxService.hotel(paramResult);
            if (!notifyPayCancel) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 退款回调(酒店)
     * 
     * @return
     */
    @RequestMapping("/hotelCancel")
    public WxCallBackBean hotelCancel() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            String refundStatus = paramResult.getRefundStatus();
            if (!WxConstant.SUCCESS.equals(refundStatus)) {
                HotelOrder order = new HotelOrder();
                order.updateInit();
                order.setNo(paramResult.getOutTradeNo());
                order.setMsgRefunds(refundStatus);
                hotelOrderDao.updateByNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayWxService.hotelCancel(paramResult);
            if (!notifyPayCancel) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 支付回调
     * 
     * @return
     */
    @RequestMapping("/notifyPayGoOn")
    public WxCallBackBean notifyPayGoOn() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            if (!WxConstant.SUCCESS.equals(paramResult.getTradeState())) {
                Order order = new Order();
                order.updateInit();
                order.setNo(paramResult.getOutTradeNo());
                order.setPayMsg(paramResult.getTradeStateDesc());
                orderDao.updatePartialByOrderNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPay = notifyPayWxService.notifyPayGoOn(paramResult);
            if (!notifyPay) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 主动退款回调
     * 
     * @return
     */
    @RequestMapping("/notifyPayCancelInitiative")
    public WxCallBackBean notifyPayCancelInitiative() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            String refundStatus = paramResult.getRefundStatus();
            if (!WxConstant.SUCCESS.equals(refundStatus)) {
                Order order = new Order();
                order.updateInit();
                order.setNo(paramResult.getOutTradeNo());
                order.setRefundsMsg(refundStatus);
                orderDao.updatePartialByOrderNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayWxService.notifyPayCancelInitiative(paramResult);
            if (!notifyPayCancel) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

    /**
     * 子订单主动退款回调
     * 
     * @return
     */
    @RequestMapping("/notifyPayCancelGoOnInitiative")
    public WxCallBackBean notifyPayCancelGoOnInitiative() {
        WxCallBackBean callBack = new WxCallBackBean();

        try {
            // 校验参数并解密密文
            WxResponseV3 paramResult = paramNofityVerifyInterface.payNofity(request);
            // 验证不通过
            if (StringUtil.isBlankObject(paramResult)) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
            String refundStatus = paramResult.getRefundStatus();
            if (!WxConstant.SUCCESS.equals(refundStatus)) {
                OrderChild order = new OrderChild();
                order.updateInit();
                order.setOrderChildSn(paramResult.getOutTradeNo());
                order.setRefundsMsg(refundStatus);
                orderChildDao.updatePartialByOrderNo(order);
                // 通知微信处理完成
                callBack.setCode(WxConstant.SUCCESS);
                return callBack;
            }

            // 处理订单
            boolean notifyPayCancel = notifyPayWxService.notifyPayCancelGoOnInitiative(paramResult);
            if (!notifyPayCancel) {
                response.setStatus(500);
                callBack.setCode(WxConstant.FAIL);
                return callBack;
            }
        } catch (Exception e) {
            // 验证不通过
            response.setStatus(500);
            callBack.setCode(WxConstant.FAIL);
            callBack.setMessage(e.getMessage());
            return callBack;
        }

        // 通知微信处理完成
        callBack.setCode(WxConstant.SUCCESS);
        return callBack;
    }

}